package com.onlineMIS.action.headQ.custMgmt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagGroupDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.custMgmt.HeadQCustDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.custMgmt.HeadQCustMgmtService;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagGroup;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagVO;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;
import com.onlineMIS.converter.JSONSQLDateConverter;
import com.onlineMIS.converter.JSONUtilDateConverter;
import com.opensymphony.xwork2.ActionContext;

public class HeadQCustMgmtJSONAction extends HeadQCustMgmtAction {
	
	private JSONObject jsonObject;
	private JSONArray jsonArray;
	private Map<String,Object> jsonMap = new HashMap<String, Object>();
	public JSONArray getJsonArray() {
		return jsonArray;
	}
	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	
	private static final long serialVersionUID = 1L;
	@Autowired
	private HeadQCustDaoImpl headQCustDaoImpl;	
	@Autowired
	private HeadQTagDaoImpl headQTagDaoImpl;
	@Autowired
	private HeadQTagGroupDaoImpl tagGroupDaoImpl;
	@Autowired
	protected HeadQCustMgmtService headQSalesService;
	
	
	/**
	 * 获取所有客户信息
	 * @return
	 */
	public String getAllCust(){
		loggerLocal.info("HeadQCustMgmtJSONAction.getAllCust");
		
		Response response = new Response();
		try {
		    response = headQSalesService.getAllCust();
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		
		if (response.getReturnCode() == Response.SUCCESS){
			
			try {
				jsonObject = JSONObject.fromObject(response);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		
		return "successful";
	}

	/**
	 * 翻页的时候查询客户信息(财务模块-客户财务管理-客户选择使用-不带标签条件的查询）
	 * 
	 * @return
	 */
	public String listCustData() {
		loggerLocal.info("HeadQCustMgmtJSONAction.listCustData");

		Response response = new Response();
		try {
			response = headQSalesService.listCust(formBean.getCust(), this.getPage(), this.getRows(), this.getSort(),
					this.getOrder());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}

		if (response.getReturnCode() == Response.SUCCESS) {

			jsonMap = (Map) response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return "successful";
	}
		

	/**
	 * 财务信息管理-分页查询客户信息(带标签条件的分页）
	 * @return
	 */
	public String listCustTagData(){
		loggerLocal.info("HeadQCustMgmtJSONAction.listCustTagData");
		
		Response response = new Response();
		try {
		    response = headQSalesService.listCustTag(formBean, this.getPage(), this.getRows(), this.getSort(), this.getOrder());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		
		return "successful";
	}
	
	
	/**
	 * 新增或者更新客户信息
	 * @return
	 */
	public String createUpdateCust(){
		loggerLocal.info("HeadQCustMgmtJSONAction.createUpdateCust");
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		Response response = new Response();
		
		try {
		    response = headQSalesService.createOrUpdateCust(formBean.getCust(),formBean.getTagGroups(),loginUserInfor, formBean.getTagId());
		} catch (Exception e) {
			e.printStackTrace();
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return "successful";
	}
	
	/**
	 * 查询客户信息不需要翻页
	 * @return
	 */
	public String searchCustData(){
		loggerLocal.info("HeadQCustMgmtJSONAction.searchCustData");
		
		Response response = new Response();
		try {
		    response = headQSalesService.searchCust(formBean.getCust());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		
		if (response.getReturnCode() == Response.SUCCESS){

			try {
				jsonObject = JSONObject.fromObject(response);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		
		return "successful";
	}
	
	
	/**
	 * 批量修改客户标签信息
	 * @return
	 */
	public String saveBatchEntityTags(){
		loggerLocal.info("HeadQCustMgmtJSONAction.saveBatchEntityTags");
		Response response = new Response();
		try {
			// 获取所有客户
			Integer ids[] = formBean.getIds();
			// 获取标签id
			String tagIdS = formBean.getTagGroups();
			
		    response = headQCustDaoImpl.saveOrUpdateEntityTags(ids,tagIdS);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		
		if (response.getReturnCode() == Response.SUCCESS){

			try {
				jsonObject = JSONObject.fromObject(response);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return "successful";

	}

	
	/**
	 * 查询客户标签分组json对象集合
	 * @return
	 */
	public String listCusTags() {
		loggerLocal.info("HeadQCustMgmtJSONAction.listCusTags");

		Response response = new Response();
		try {
			List<HeadQTag> list = new ArrayList<HeadQTag>();
			List<HeadQTagVO> vos = new ArrayList<HeadQTagVO>();
			//获取分组
			List<Integer> groups = tagGroupDaoImpl.selectByGroup(HeadQTagGroup.GROUP_ONE);
			if (groups.size() !=0) {
				for (Integer groupId : groups) {
					List<HeadQTag> tags = headQTagDaoImpl.getLevelOne(groupId);
					list.addAll(tags);
				}
			}
			for (HeadQTag tag : list) {
				HeadQTagVO vo = new HeadQTagVO(tag);
				vos.add(vo);
			}
			JsonConfig jsonConfig = new JsonConfig();
    		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  
    		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());  
			jsonArray = JSONArray.fromObject(vos,jsonConfig);
					
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}

		return "jsonArray";
	}
	

	
	/**
	 * 冻结某个账户
	 * @return
	 */
//	public String disbleCust(){
//		loggerLocal.info("HeadQSalesJSONAction.disbleCust");
//		Response response = new Response();
//		
//		try {
//		    response = headQSalesService.disableCust(formBean.getCust().getId());
//		} catch (Exception e) {
//			loggerLocal.error(e);
//			response.setReturnCode(Response.FAIL);
//		}
//
//	    jsonObject = JSONObject.fromObject(response);
//		
//		return "successful";
//	}
	
	
}
